//Editor de codigo
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import org.w3c.dom.*;

public class editorcodigo extends JPanel{
 private Style estiloRojo,estiloVerde,estiloAzul;
 private JTextPane texto;
 StyleContext sc = new StyleContext();
 DefaultStyledDocument doc = new DefaultStyledDocument(sc);
 MutableAttributeSet standard = new SimpleAttributeSet();
 private xml reader;
 private String[] reservadas,simbolos;
 private int cr,cs;
 
 public editorcodigo(){
  setLayout( new BorderLayout() );
  texto=new JTextPane(doc);
  //setStyledDocument(StyledDocument doc) 
  StyleConstants.setForeground(standard, Color.black);
  doc.setCharacterAttributes(0,texto.getText().length(),standard, false);
  final MutableAttributeSet characters = new SimpleAttributeSet();
  StyleConstants.setForeground(characters, Color.green.darker());
  reader=new xml("reservadas.xml");
  reader.addFiltro("tipo","datatipo");
  reservadas=reader.obtItems("TOKEN");
  cr=reader.getLength();
  reader.updFiltro("tipo","simbolo");
  simbolos=reader.obtItems("TOKEN");
  cs=reader.getLength();
  texto.addKeyListener(new KeyAdapter(){
    public void keyPressed(KeyEvent e) {
    }
    public void keyReleased(KeyEvent e) {
     SimpleAttributeSet atributo = new SimpleAttributeSet();
     StyleConstants.setFontSize(atributo,12);
     StyleConstants.setForeground(atributo,Color.blue);
     SimpleAttributeSet atributo2 = new SimpleAttributeSet();
     StyleConstants.setFontSize(atributo2,12);
     StyleConstants.setForeground(atributo2,Color.black);
     SimpleAttributeSet atributo3 = new SimpleAttributeSet();
     StyleConstants.setFontSize(atributo3,12);
     StyleConstants.setForeground(atributo3,Color.green);
     String soriginal=texto.getText();
     int post=0;
     texto.getStyledDocument().setCharacterAttributes(0,soriginal.length(),atributo2,false);
     texto.setCharacterAttributes(atributo2,false);
     for(int r=0;r<cr;r++) {
      post=soriginal.indexOf(reservadas[r]);
      while (post > -1) {
       texto.getStyledDocument().setCharacterAttributes(post,post+reservadas[r].length(),atributo,false);
       texto.setCharacterAttributes(atributo,false);
       texto.getStyledDocument().setCharacterAttributes(post+reservadas[r].length(),post+reservadas[r].length()+1,atributo2,false);
       texto.setCharacterAttributes(atributo2,false);
       post=soriginal.indexOf(reservadas[r],post+1);
      }
     }
     for(int s=0;s<cs;s++) {
      post=soriginal.indexOf(simbolos[s]);
      while (post > -1) {
       texto.getStyledDocument().setCharacterAttributes(post,post+simbolos[s].length(),atributo3,false);
       texto.setCharacterAttributes(atributo3,false);
       texto.getStyledDocument().setCharacterAttributes(post+simbolos[s].length(),post+simbolos[s].length()+1,atributo2,false);
       texto.setCharacterAttributes(atributo2,false);
       post=soriginal.indexOf(simbolos[s],post+1);
      }
     }
     
    }
   }
  );
  add( texto,BorderLayout.CENTER );
 } 

 /*public static void main( String argv[] ) {
  JFrame ventana = new JFrame( "Mi editor de codigo" );
  ventana.addWindowListener( new WindowAdapter() {
   public void windowClosing( WindowEvent evt ){
    System.exit( 0 );
   }
  });
  ventana.getContentPane().add( new editorcodigo(),BorderLayout.CENTER );
  ventana.setSize( 300,180 );
  ventana.setVisible( true );
 }*/
 
}
